'use strict';

define(['controllers', '../service/statService', './filterController', 'highcharts'], function (controllers) {
    controllers.stat.register('QpsController', ['$scope', '$rootScope', 'StatService', function ($scope, $rootScope, statService) {
        $scope.onlyNode = false;

        $scope.reloadChart = function(divId, title, stat, width) {
            var series = [];
            for (var i = 0; i < stat.series.length; i++) {
                var serie = {
                    name: stat.series[i].name,
                    data: []
                };
                for (var key in stat.series[i].data) {
                    serie.data.push([key - 0, stat.series[i].data[key]]);
                }
                series.push(serie);
            }
            var startTime = stat.categories[0];
            var endTime = stat.categories[stat.categories.length - 1];
            var tickInterval = (new Date(endTime).getDayEndTime() + 1 - startTime) / 12;
            return new Highcharts.Chart({
                chart: {
                    type: 'spline',
                    renderTo: divId,
                    width: width
                },
                title: {
                    text: title,
                    x: -20
                },
                xAxis: {
                    type: 'datetime',
                    labels: {
                        formatter: function () {
                            return $rootScope.formatStatTime(this.value, $scope.param.statType);
                        }
                    }
                },
                plotOptions: {
                    spline: {
                        lineWidth: 1,
                        states: {
                            hover: {
                                lineWidth: 3
                            }
                        },
                        marker: {
                            enabled: false
                        },
                        pointInterval: tickInterval,
                        pointStart: startTime
                    }
                },
                yAxis: {
                    title: {
                        text: 'QPS'
                    },
                    min: 0
                },
                tooltip: {
                    formatter: function () {
                        return  '<b>' + this.series.name + '</b><br/>' +
                            $rootScope.formatStatTime(this.x, $scope.param.statType) + '<br/>' +
                            'QPS:' + this.y.toFixed(2);
                    }
                },
                series: series
            });
        };

        $scope.getStat = function (param) {
            $scope.param = param;
            statService.getQpsStat(param, function (stat) {
                var avgStat = stat['avg'];
                var maxStat = stat['max'];

                //数据图表
                var chartWidth = Math.max($("#avg_chart_panel").width(), $('#max_chart_panel').width(), $('#list_panel').width());
                if ($scope.avgChart != null) {
                    $scope.avgChart.destroy();
                }
                $scope.avgChart = $scope.reloadChart('avgChart', '平均QPS统计', avgStat, chartWidth);

                if ($scope.maxChart != null) {
                    $scope.maxChart.destroy();
                }
                $scope.maxChart = $scope.reloadChart('maxChart', '最大QPS统计', maxStat, chartWidth);

                //数据列表
                var avgList = {};
                for (var i = 0; i < avgStat.series.length; i++) {
                    var avgSerie = avgStat.series[i];
                    avgList[avgSerie.name] = {};
                    for (var j = 0; j < avgStat.categories.length; j++) {
                        var categorie = avgStat.categories[j];
                        avgList[avgSerie.name][categorie] = avgSerie.data[categorie];
                    }
                }

                var maxList = {};
                for (var i = 0; i < maxStat.series.length; i++) {
                    var maxSerie = maxStat.series[i];
                    maxList[maxSerie.name] = {};
                    for (var j = 0; j < maxStat.categories.length; j++) {
                        var categorie = maxStat.categories[j];
                        maxList[maxSerie.name][categorie] = maxSerie.data[categorie];
                    }
                }

                var list = {};
                for (var name in avgList) {
                    list[name] = {};
                    for (var categorie in avgList[name]) {
                        var time = new Date(categorie - 0).format('yyyy-MM-dd HH:mm:ss');
                        list[name][time] = {};
                        list[name][time]['avg'] = avgList[name][categorie];
                        list[name][time]['max'] = maxList[name][categorie];
                    }
                }

                $scope.list = list;
            });
        };

        $scope.getHomeQpsStat = function() {
            $scope.param = {
                dimension: $rootScope.DIMENSION_COLLECTION,
                statType: $rootScope.STAT_TYPE_HOUR,
                startTime: new Date(new Date().getTime() - 24 * 3600 * 1000).getDayBeginTime(),
                endTime: new Date(new Date().getTime() - 24 * 3600 * 1000).getDayEndTime()
            };
            statService.getQpsStat($scope.param, function (stat) {
                if ($scope.homeChart != null) {
                    $scope.homeChart.destroy();
                }
                $scope.homeChart = $scope.reloadChart('qpsChart', '平均QPS统计', stat['avg']);
            });
        };

        $scope.getOverviewQpsStat = function (collectionId) {
            $scope.param = {
                dimension: $rootScope.DIMENSION_REPLICA,
                statType: $rootScope.STAT_TYPE_HOUR,
                collectionId: collectionId,
                startTime: new Date(new Date().getTime() - 24 * 3600 * 1000).getDayBeginTime(),
                endTime: new Date(new Date().getTime() - 24 * 3600 * 1000).getDayEndTime()
            };
            statService.getQpsStat($scope.param, function (stat) {
                if ($scope.overviewQpsChart != null) {
                    $scope.overviewQpsChart.destroy();
                }
                $scope.overviewQpsChart = $scope.reloadChart('qpsChart', 'QPS统计', stat['avg']);
            });
        };
    }]);
});